﻿using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Controls;
using WP7Client.Controller;
using WP7Client.Helpers;
using WP7Client.Managers;
using System.Collections.Generic;
using WP7Client.Classes;

namespace WP7Client.UserInterface.Pages
{
    public partial class OverviewPage : MSHPhoneApplicationPage
    {
        private static bool toLogin = false;

        public OverviewPage()
        {
            InitializeComponent();
            ControllerHelper.Startup(this);
        }

        private void MSHPhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext != null && NavigationContext.QueryString != null && NavigationContext.QueryString.ContainsKey("PivotItemName"))
            {
                String itemName = null;
                NavigationContext.QueryString.TryGetValue("PivotItemName", out itemName);
                if (!String.IsNullOrEmpty(itemName))
                    SelectPivotItem(itemName);
            }

            DataContext = ControllerHelper.OverviewController;
            PivotItemFriends.DataContext = ControllerHelper.FriendController;
            ButtonFriendsMap.DataContext = ControllerHelper.FriendController;
            PivotItemSearch.DataContext = ControllerHelper.SearchController;
            PivotItemSettings.DataContext = ControllerHelper.SettingController;
            PivotItemConverstations.DataContext = ControllerHelper.ConversationsController;
            PivotItemPeopleNearby.DataContext = ControllerHelper.PeopleNearbyController;
            ButtonPeopleNearbyMap.DataContext = ControllerHelper.PeopleNearbyController;
            PivotItemSearch.DataContext = ControllerHelper.SearchController;
            PivotItemInvitations.DataContext = ControllerHelper.InvitationController;
            PivotItemMyProfile.DataContext = ControllerHelper.MyProfileController;
            PivotItemSocialNetworks.DataContext = ControllerHelper.SocialNetworkAccountController;
            PivotItemSuggestions.DataContext = ControllerHelper.SuggestionController;
        }

        private void SelectPivotItem(String name)
        {
            PivotItem item = GetPivotItem(name);
            if (item != null)
                thePivot.SelectedItem = item;
        }

        private PivotItem GetPivotItem(String name)
        {
            foreach(PivotItem item in thePivot.Items)
            {
                if (item.Name.Equals(name))
                    return item;
            }
            return null;
        }
    }
}
